﻿using System;
using System.CodeDom.Compiler;
using System.Diagnostics;
using EnvDTE;
using Microsoft.VisualStudio.Designer.Interfaces;
using Microsoft.VisualStudio.Project;
using Microsoft.VisualStudio.Project.Automation;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.WowAddonStudio.Lua.CodeDom;
using Microsoft.WowAddonStudio.Lua.CodeDom.Providers;

namespace Microsoft.WowAddonStudio.Project.Automation
{
    public class OAWowLuaFileItem : OAWowFileItem
    {
        private LuaFileCodeModel codeModel;

        public OAWowLuaFileItem(OAProject project, FileNode node)
            : base(project, node)
        {
        }


        /// <summary>
        /// Gets FileCodeModel for WowProjectItem
        /// </summary>
        public override FileCodeModel FileCodeModel
        {
            get
            {
                try
                {
                    if (null != codeModel) 
                        return codeModel;

                    var sp = new ServiceProvider(Node.OleServiceProvider);
                    var smdProvider = sp.GetService(typeof(SVSMDCodeDomProvider)) as IVSMDCodeDomProvider ?? new VSMDLuaCodeDomProvider(this);
                    var provider = smdProvider.CodeDomProvider as CodeDomProvider;

                    codeModel = LuaCodeModelFactory.CreateFileCodeModel(this, provider, Node.Url);

                    return codeModel;
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex);
                    throw;
                }
            }
        }

    }
}
